package foundation.pcmUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

import foundation.fileUtil.XMLFileUtil;

public class RptFileHelper {
	
	private XMLFileUtil xmlHelper;
	private String rptFileName;

	public RptFileHelper(){
		
	}
	
	public RptFileHelper(String rptFileName){
		this.rptFileName = rptFileName;
		File rptFile = new File(rptFileName);
		xmlHelper = new XMLFileUtil(rptFile);	
	}
	
	/**
	 * 根据使用场景获取响应时间
	 * @param usageScenarioProer:使用场景的名字
	 * @return
	 */
	public float getRest(String usageScenarioProer){
		float resTime = 0f;
		try {
			String path = "/lqn-model/processor[@name='" + usageScenarioProer + "']/" + "task/entry/result-entry";
			Element timeEle = xmlHelper.readNode(path).get(0);
			resTime = Float.valueOf(timeEle.getAttributeValue("phase1-service-time"));
		} catch (JDOMException e) {
			e.printStackTrace();
		}
		return resTime;
	}
	
	/**
	 * 获取部署有组件的四个server解析成的processor
	 * @return
	 */
	public List<String> getUsedProcesssors(){
		
		List<String> proNames = new ArrayList<String>();
		String path="/lqn-model/processor";
			
		try {
			List<Element> proEles = xmlHelper.readNode(path);
			for(Element proEle:proEles){
				float util = Float.valueOf(proEle.getChildren().get(0)
						.getAttributeValue("utilization"));
				String proEleName = proEle.getAttributeValue("name");
				if(Float.compare(util,0f)>0&&(!proEleName.startsWith("LinkingResource_"))){
					proNames.add(proEleName);
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return proNames;
	}
	
	/**
	 * 根据一组processor名字，找到最大使用率的server名字
	 * @param proNames List<String> proNames
	 * @return
	 */
	public String getMaxUtilSvr(){
		List<String> proNames = getUsedProcesssors();
		String svrName = null;
		List<Float> proUtils = new ArrayList<Float>();
		try {
			for (String proceor : proNames) {
				String path = "/lqn-model/processor[@name='" + proceor
						+ "']/result-processor";
				Element proNameEle = xmlHelper.readNode(path).get(0);
				float util = Float.valueOf(proNameEle
						.getAttributeValue("utilization"));
				proUtils.add(util);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int index = 0;
		float maxVal = proUtils.get(0);
		for(int i=1;i<proUtils.size();i++){
			float temp = proUtils.get(i);
			if(Float.compare(temp,maxVal)>0){
				maxVal = temp;
				index = i;
			}
		}
		String proName = proNames.get(index);
		svrName = proName.substring(0,proName.indexOf("_"));

		return svrName;
	}
	
	/**
	 * 根据一组processor名字，找到最小使用率（不等于0）的server名字
	 * @param proNames
	 * @return
	 */
	public String getMinUtilSvr(){
		List<String> proNames = getUsedProcesssors();
		String svrName = null;
		List<Float> proUtils = new ArrayList<Float>();
		try {
			for (String proceor : proNames) {
				String path = "/lqn-model/processor[@name='" + proceor
						+ "']/result-processor";
				Element proNameEle = xmlHelper.readNode(path).get(0);
				float util = Float.valueOf(proNameEle
						.getAttributeValue("utilization"));
				proUtils.add(util);
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int index = 0;
		float minVal = proUtils.get(0);
		for(int i=1;i<proUtils.size();i++){
			float temp = proUtils.get(i);
			if(Float.compare(temp,minVal)<0){
				minVal = temp;
				index = i;
			}
		}
		String proName = proNames.get(index);
		svrName = proName.substring(0,proName.indexOf("_"));

		return svrName;
	}
	
	/**
	 * 找到部署在某个server上使用率最小的组件
	 * @param compNames
	 * @return
	 */
	public String getMinUtilComp(List<String> compNames){
		String compName = null;
		String path="/lqn-model/processor";
		List<Float> compTaskUtils = new ArrayList<Float>();
		float sumTaskUtil = 0f;
		try {
			for (String comp : compNames) {
				List<Element> proEles = xmlHelper.readNode(path);
				for(Element proEle:proEles ){
					String proName = proEle.getAttributeValue("name");
					if(proName.indexOf(comp+"_")!=-1){
						String taskPath = "/lqn-model/processor[@name='"+ proName+"']/"
								+ "task/result-task";
						Element taskUtilEle = xmlHelper.readNode(taskPath).get(0);
						float taskUtil = Float.valueOf(taskUtilEle.getAttributeValue("utilization"));	
						sumTaskUtil+=taskUtil;
					}					
				}
				compTaskUtils.add(sumTaskUtil);
				sumTaskUtil = 0f;
			}
			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int index = 0;
		float minVal = compTaskUtils.get(0);
		for(int i=1;i<compTaskUtils.size();i++){
			float temp = compTaskUtils.get(i);
			if(Float.compare(temp,minVal)<0){
				minVal = temp;
				index = i;
			}
		}
		compName = compNames.get(index);
		
		return compName;
	}
	
	/**
	 * 根据server名字获取使用率
	 * @param svrName
	 * @return
	 */
	public Float getSvrUtil(String svrName){
		float svrUtil = 0f;
		List<String> proNames = getUsedProcesssors();
		try {
			for (String proName : proNames) {
				if (proName.substring(0, proName.indexOf("_")).equals(svrName)) {
					String path = "/lqn-model/processor[@name='" + proName + "']";
					Element proEle = xmlHelper.readNode(path).get(0);
					svrUtil = Float.valueOf(proEle.getChildren().get(0).getAttributeValue("utilization"));
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return svrUtil; 
	}
	
	public void closeNoSave(){
		xmlHelper.close(rptFileName);
	}
	
	public XMLFileUtil getXmlHelper() {
		return xmlHelper;
	}

	public void setXmlHelper(XMLFileUtil xmlHelper) {
		this.xmlHelper = xmlHelper;
	}

	public String getRptFileName() {
		return rptFileName;
	}

	public void setRptFileName(String rptFileName) {
		this.rptFileName = rptFileName;
	}

}
